package org.ucas.faker.web.service.graph;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
public class AdvancedStatService {




    /**
     * 匹配关键词与专利文档的权重
     */
    protected double getWeight(String word, Map<String, Double> wordMap, Map<String, Object> patentMap){
        String abstraction = "";
        String title = "";
        String claim = "";

        if (patentMap.get("abstraction") != null)
            abstraction = patentMap.get("abstraction").toString();

        if (patentMap.get("title") != null)
            title = patentMap.get("title").toString();

        if (patentMap.get("primary_claim") != null)
            claim = patentMap.get("primary_claim").toString();

        double weight = 0.0;
        double abstractionWeight = 2.0;
        double titleWeight = 5.0;
        double claimWeight = 1.3;

        Pattern pattern = Pattern.compile(word.toLowerCase());

        Matcher matcher = pattern.matcher(abstraction);
        while (matcher.find()) weight += abstractionWeight;

        matcher = pattern.matcher(title);
        while (matcher.find()) weight += titleWeight;

        matcher = pattern.matcher(claim);
        while (matcher.find()) weight += claimWeight;

        int count = 0;
        String document = title+" "+abstraction+" "+claim;
        for (String tmp : wordMap.keySet()){
            if (!tmp.equals(word)){
                if (document.indexOf(tmp) != -1){
                    count ++;
                }
            }
        }
//        if (count<=1) weight = 0; //TODO count <= 3
        return weight;
    }
}
